import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求中的姓名和密码
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        //判断姓名和密码是否正确
        if(("zhangsan").equals(username)&&("123").equals(password)){
            //创建会话
            HttpSession httpSession=req.getSession(true);
            //如果会话不存在，可以自动创建一个会话
            //往会话中存键值对，保存重要信息
            resp.setContentType("text/html;charset=utf8");
            httpSession.setAttribute("username",username);
            httpSession.setAttribute("count",0);
            //跳转到index页面
            resp.sendRedirect("index");
        }else{
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户名或者密码错误");
        }
    }
}
